package stuy.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@WebServlet(name = "SessionServlet01", urlPatterns = "/session01")
public class SessionServlet01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //如果可以使用已有session,则使用已有session
        //如果没有创建session ,则会自动创建一个session
        //并分配JSessionID
        HttpSession session = request.getSession();
        // 获取创建时间
        //java 中 时间是从1970年 开始 经过的毫秒数
        long time = session.getCreationTime();
        Date date = new Date(time);
        int year = date.getYear();//年
        int month = date.getMonth();//月
        int day = date.getDay();//日
        SimpleDateFormat sdf =
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(new Date(time)));
        // 获取 SessionID
        //自己去手动设置jSessionID的超时时间
        String sessionId = session.getId();
        Cookie cookie = new Cookie("JSESIONID", sessionId);
        cookie.setMaxAge(7 * 24 * 3600);//一周的超时时间
        cookie.setPath("/");
        response.addCookie(cookie);
        //手动设置这一个session的超时时间
        //不会影响其他session,单位是秒!
        session.setMaxInactiveInterval(30);

        //立即销毁session
        //注销功能
        session.invalidate();

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
